<?php

/**
 * @filename source_module.php 2013-11-11 02:10:56x
 * @author Huming Xu <huming17@126.com>
 * @version 1.0.0
 * @copyright DZF (c) 2013, Huming Xu
 */
class ctrl_index_index extends index_index {

    public $info_array = array();
    public $page_array = array();
    public $tree_array = array();

    static function &instance() {
        static $object;
        if (empty($object)) {
            $object = new self();
        }
        return $object;
    }

    /*
     *  系统信息列表
     */

    public function do_index() {
        global $_G;
        $api = isset($_REQUEST['api']) ? $_REQUEST['api'] : 'json';       
        //网校id
        if($_G['member']['role_id']==82){  //网校理员权限
        	$so_id = $this->one_so('user_detail', 'user_id', $_G['user_id']);       	
        }else{
        	$so_id = isset($_REQUEST['so_id']) ? $_REQUEST['so_id'] : '0';
        }
        //DEBUG 获取首页轮播图片 开始
        $page_condition = array(
            'page' => 1, //int 请求页面 页码
            'limit' => 20, //int 每页请求个数
            'perpage' => 20, //int 每页显示个数
            'wheresql' => ' AND ac_id=1 AND isdelete=0 AND ai_status=1  AND so_id ='.$so_id.' ', //string //条件SQL语句 
            'orderby' => 'ORDER BY ai_id ASC', //string 排序规则
        );
        $page_result = $this->api_index('ad_info', $page_condition);
        //$sql = "SELECT * FROM ".DB::table('ad_info')." WHERE ac_id=1 AND isdelete=0 AND ai_status=1 AND ai_belong=0 ORDER BY ai_sort ASC LIMIT 3";
        //$result = DB::fetch_all($sql);
        if($page_result['page_data']){
	        foreach ($page_result['page_data'] AS $key => $value) {
	            $tmp = array("id" => '', "color" => '', "url" => '', "img" => '', "name" => '');
	            $tmp['id'] = $value['ai_id'];
	            $tmp['color'] = $value['ai_bgcolor'];
	            $tmp['url'] = $value['ad_link'];
	            $tmp['img'] = $value['ai_path'];
	            $tmp['name'] = $value['ai_name'];
	            $data['slide'][] = $tmp;
	        }
        }else{
        	$data['slide']=[];
        }
        //DEBUG 获取首页轮播图片 结束
        //DEBUG 获取首页广告图片 开始
      
        	$page_condition = array(
        			'page' => 1, //int 请求页面 页码
        			'limit' => 20, //int 每页请求个数
        			'perpage' => 20, //int 每页显示个数
        			'wheresql' => ' AND ac_id IN (2,3) AND isdelete=0 AND ai_status=1  AND so_id ='.$so_id.' ', //string //条件SQL语句
        			'orderby' => 'ORDER BY place_id ASC,ai_id ASC', //string 排序规则
        	);
       
        $page_result = $this->api_index('ad_info', $page_condition);
        //$sql = "SELECT * FROM ".DB::table('ad_info')." WHERE ac_id IN (2,3) AND isdelete=0 AND ai_status=1 AND ai_belong=0 ORDER BY ac_id ASC,ai_sort ASC LIMIT 5";
        //$result = DB::fetch_all($sql);
        if($page_result['page_data']){
	        foreach ($page_result['page_data'] AS $key => $value) {
	            $tmp = array("id" => '', "img" => '', "url" => '');
	            $tmp['id'] = $value['place_id'];
	            $tmp['name'] = $value['ai_name'];
	            $tmp['img'] = $value['ai_path'];
	            $tmp['color'] = $value['ai_bgcolor'];
	            $tmp['url'] = $value['ad_link'];
	            $data['ad'][] = $tmp;
	        }
        }else{
        	$data['ad']=[];
        }
        //DEBUG 获取首页广告图片 结束
        //DEBUG 获取专题课程 开始
        if($so_id==0){	      
	        $page_condition = array(
	            'page' => 1, //int 请求页面 页码
	            'limit' => 30, //int 每页请求个数
	            'perpage' => 30, //int 每页显示个数
	            'wheresql' => ' AND isfrontpage =1 AND isdelete=0 ', //string //条件SQL语句 
	            'orderby' => 'ORDER BY frontpage_sort ASC', //string 排序规则
	        );
	        $page_result = $this->api_index('course_topic', $page_condition);
	        if($page_result['page_data']){
		        foreach ($page_result['page_data'] AS $key => $value) {
		            $tmp = array("id" => '', "img" => '', "url" => '', "title" => '', "sub" => '');
		            $tmp['id'] = $value['ct_id'];
		            $tmp['img'] = $value['cg_img_small'];
		            $tmp['url']=$value['ct_url'];
		            $tmp['title'] = $value['cg_name'];
		            $tmp['sub'] = $value['cg_name_sub'];
		            $data['subject'][] = $tmp;
		        }
	        }else{
	        	$data['subject']=[];
	        }
        }
        //DEBUG 获取专题课程 结束
        //DEBUG 获取楼层课程 开始
        $page_condition = array(
            'page' => 1, //int 请求页面 页码
            'limit' => 30, //int 每页请求个数
            'perpage' => 30, //int 每页显示个数
            'wheresql' => " AND as_position ='floor' AND isdelete=0 AND so_id =".$so_id.' ', //string //条件SQL语句 
            'orderby' => 'ORDER BY as_id ASC', //string 排序规则
        );
        $page_result = $this->api_index('api_setting', $page_condition);
        $floor = $as_ids = array();
        foreach ($page_result['page_data'] AS $key => $value) {
            $tmp = json_decode($value['as_setting'], true);
            $as_ids[$value['as_id']] = $value['as_id'];
            $floor[$value['as_id']] = $tmp;
            $floor[$value['as_id']]['as_id']=$value['as_id'];
            $floor[$value['as_id']]['as_sort']=$value['as_sort'];            
        }
        $page_condition = array(
            'page' => 1, //int 请求页面 页码
            'limit' => 30, //int 每页请求个数
            'perpage' => 30, //int 每页显示个数
            'wheresql' => " AND as_id IN (" . dimplode($as_ids) . ") AND isdelete=0 ", //string //条件SQL语句 
            'orderby' => 'ORDER BY am_sort ASC', //string 排序规则
        );
        $page_result = $this->api_index('api_main', $page_condition);
        $cm_ids = array();
        foreach ($page_result['page_data'] AS $key => $value) {
            $tmp = array("id" => '', "title" => '', "price" => '', "teacher" => '', "times" => '', "img" => '', "am_sort" => '');
            $course_info = $this->one_so_info('course_main', 'cm_id', $value['am_data'],$so_id);
            if (!empty($course_info)) {
                $tmp = array("id" => $course_info['cm_id'], "title" => $course_info['cm_name'], "price" => $course_info['cm_price'], "teacher" => ext::user_realname($course_info['user_id']), "times" => $course_info['cm_hour_num'], "img" => $course_info['cm_img'], "am_sort" => $value['am_sort']);
                $floor[$value['as_id']]['course'][] = $tmp;
            }
        }
        if($floor){
	        foreach ($floor AS $key => $value) {
	            $data['floor'][] = $value;
	        }
        }else{
        	$data['floor']=[];
        }
        //DEBUG 获取楼层课程 结束
        //DEBUG 获取明星讲师 开始
        if($so_id==0){	       
	        $page_condition = array(
	            'page' => 1, //int 请求页面 页码
	            'limit' => 30, //int 每页请求个数
	            'perpage' => 30, //int 每页显示个数
	            'wheresql' => ' AND flag =1 AND isdelete=0 ', //string //条件SQL语句 
	            'orderby' => 'ORDER BY id DESC,frontpage_sort ASC', //string 排序规则
	        );
	        $page_result = $this->api_index('famous_teachers', $page_condition);
	        if($page_result['page_data']){
		        foreach ($page_result['page_data'] AS $key => $value) {
		            $tmp = array("id" => '', "img" => '', "name" => '', "title" => '', "content" => '');
		            $tmp['id'] = $value['teacher_id'];
		            //$tmp['teacher_id'] = $value['teacher_id'];
		            $tmp['img'] = $value['avatar'];
		            $tmp['name'] = $value['teacher_name'];
		            $tmp['title'] = $value['brife'];
		            $tmp['content'] = $value['information'];
		            $data['teacher'][] = $tmp;
		        }
	        }else{
	        	$data['teacher']=[];
	        }
        }
        //DEBUG 获取明星讲师 结束        
        //DEBUG 获取公告通知 开始
        if($so_id){
	        $page_condition = array(
	            'page' => 1, //int 请求页面 页码
	            'limit' => 10, //int 每页请求个数
	            'perpage' => 3, //int 每页显示个数
	            //'wheresql' => ' AND info_cateid=1006 AND isdelete=0 AND so_id ='.$so_id.' AND isfrontpage=1 ', //string //条件SQL语句 
	            'wheresql' => ' AND info_cateid=1006 AND isdelete=0 AND isfrontpage=1 AND so_id = '.$so_id, //string //条件SQL语句 
	            'orderby' => 'ORDER BY info_id DESC,frontpage_order ASC', //string 排序规则
	        );  
        }else{
        	$page_condition = array(
        			'page' => 1, //int 请求页面 页码
        			'limit' => 10, //int 每页请求个数
        			'perpage' => 3, //int 每页显示个数
        			//'wheresql' => ' AND info_cateid=1006 AND isdelete=0 AND so_id ='.$so_id.' AND isfrontpage=1 ', //string //条件SQL语句
        			'wheresql' => ' AND info_cateid=1006 AND isdelete=0 AND isfrontpage=1 ', //string //条件SQL语句
        			'orderby' => 'ORDER BY info_id DESC,frontpage_order ASC', //string 排序规则
        	);
        }
        $page_result = $this->api_index('content_info', $page_condition);
        if($page_result['page_data']){
	        foreach ($page_result['page_data'] AS $key => $value) {
	            $tmp = array("id" => '', "title" => '', "url" => '', "time" => '');
	            $tmp['id'] = $value['info_id'];
	            $tmp['title'] = $value['title'];
	            //$tmp['url']='TODO url='.$value['info_id'];
	            $tmp['time'] = date('Y-m-d', $value['create_dateline']);
	            $tmp['create_dateline'] = $value['create_dateline'];
	            $data['notice'][] = $tmp;
	        }
        }else{
        	$data['notice']=[];
        }
        if($so_id){
        	$page_condition = array(
        			'page' => 1, //int 请求页面 页码
        			'limit' => 50, //int 每页请求个数
        			'perpage' => 50, //int 每页显示个数
        			'wheresql' => " AND cm_flag =1 AND cm_recommend =1 AND isdelete=0  and so_id = ".$so_id, //string //条件SQL语句
        			'orderby' => ' order by cm_count DESC', //string 排序规则
        	);
        	$course_spread = $this->api_index('course_main', $page_condition);
        	if($course_spread['page_data']){
        		foreach ($course_spread['page_data'] AS $key => $value) {
        			$tmp = array("type" => '', "startTime" => '', "title" => '', "price" => '', "school" => '', "id" => '', "img" => '');
        			$tmp['type'] = $value['cm_cate_id'];
        			$tmp['startTime'] = date('Y-m-d', $value['cm_start_time']);
        			$tmp['title'] = $value['cm_name'];
        			$tmp['price'] = $value['cm_price'];
        			$school_online = $this->one_info('school_online', 'so_id', $value['so_id']);
        			$tmp['school'] = $school_online['so_name'];
        			$tmp['id'] = $value['cm_id'];
        			if($value['cm_img']&&$value['cm_img']!='http://webapi.jxzhjy.com/'){
        				$tmp['img'] = $value['cm_img'];
        			}else{
        				$tmp['img'] = "http://webapi.jxzhjy.com/data/schoolimg/default_img.jpg";
        			}
        			$tmp['is_group'] = $value['is_group'];
        			$data['spread'][] = $tmp;
        		}
        	}else{
        		$data['spread']=array();
        	}
        }
        //DEBUG 获取公告通知 结束
        if ($api == 'json') {
            /*
              {
              slide: [
              { color: '#FFFFFF', url: 'http://www.baidu.com', img: 'images/1190.png', name: '1' },
              { color: '#FFF68F', url: 'http://www.baidu.com', img: 'http://web.img.chuanke.com/fragment/eec6d17f76fd52a14b9b97b500535891.jpg', name: '2' },
              { color: '#EEAEEE', url: 'http://www.baidu.com', img: 'http://web.img.chuanke.com/fragment/7a0f22a4a9e2e2efe874bc59340b3d83.jpg', name: '3' }
              ],
              ad: [
              { img: 'images/610.png', url: 'http://www.baidu.com' },
              { img: 'images/250.png', url: 'http://www.baidu.com' },
              { img: 'images/124.png', url: 'http://www.baidu.com' },
              { img: 'images/124.png', url: 'http://www.baidu.com' },
              { img: 'images/310.png', url: 'http://www.baidu.com' }
              ],
              subject: [
              { img: 'images/93.png', url: 'http://www.baidu.com', title: '百度校园-相约好课', sub: '最前沿的技术解密，最清晰的人才职业导航。' },
              { img: 'images/93.png', url: 'http://www.baidu.com', title: '深入浅出-对话大数据', sub: '热点案例，轻松讲解，再不懂点大数据你就OUT了！' },
              { img: 'images/93.png', url: 'http://www.baidu.com', title: '周周相约 趴趴精彩', sub: '精心打造周末充电趴，蜕变从利用好每个周末开始，我们已备好，约么？' }
              ],
              floor: [{
              course: [
              { title: 'Java语言编程基础', price: '100', teacher: 'jinxc', times: '10', url: 'http://www.baidu.com', img: 'images/160.png' },
              { title: 'Java语言编程基础', price: '100', teacher: 'jinxc', times: '10', url: 'http://www.baidu.com', img: 'images/160.png' },
              { title: 'Java语言编程基础', price: '100', teacher: 'jinxc', times: '10', url: 'http://www.baidu.com', img: 'images/160.png' },
              { title: 'Java语言编程基础', price: '100', teacher: 'jinxc', times: '10', url: 'http://www.baidu.com', img: 'images/160.png' }
              ],
              title: '精品课',
              url: 'http://www.baidu.com',
              type: 1
              }, {
              course: [
              { title: 'Java语言编程基础', price: '100', teacher: 'jinxc', times: '10', url: 'http://www.baidu.com', img: 'images/160.png' },
              { title: 'Java语言编程基础', price: '100', teacher: 'jinxc', times: '10', url: 'http://www.baidu.com', img: 'images/160.png' },
              { title: 'Java语言编程基础', price: '100', teacher: 'jinxc', times: '10', url: 'http://www.baidu.com', img: 'images/160.png' },
              { title: 'Java语言编程基础', price: '100', teacher: 'jinxc', times: '10', url: 'http://www.baidu.com', img: 'images/160.png' }
              ],
              title: '精品课',
              url: 'http://www.baidu.com',
              type: 2
              }],
              teacher: [
              { id: '1', img: 'images/50.png', url: 'http://www.baidu.com', name: '百度校园-相约好课', title: '最前沿的技术解密，最清晰的人才职业导航。','content':''},
              { id: '2', img: 'images/50.png', url: 'http://www.baidu.com', name: '深入浅出-对话大数据', title: '热点案例，轻松讲解，再不懂点大数据你就OUT了！','content':''},
              { id: '3', img: 'images/50.png', url: 'http://www.baidu.com', name: '周周相约 趴趴精彩', title: '精心打造周末充电趴，蜕变从利用好每个周末开始，我们已备好，约么s？','content':''}
              ],
              notice: [
              { title: '内容1', url: 'http://www.baidu.com', time: '2015-01-02' },
              { title: '内容2', url: 'http://www.baidu.com', time: '2015-01-02' },
              { title: '内容3', url: 'http://www.baidu.com', time: '2015-01-02' }
              ]
              }
             */
            $return['code'] = '0';
            $return['data'] = $data;
            echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
        }
        //include template('index/index');	
    }

    /**
     * 接口 AJAX或外部请求检索数据
     *
     * @param $return_type string json/xml.
     *
     * @return print string json/xml.
     */
    public function do_api() {
        $return = '';
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start = (($page - 1) * $perpage);
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        $return_type = isset($_REQUEST['return_type']) ? $_REQUEST['return_type'] : 'json';
        $wheresql = "";
        if ($keyword) {
            $wheresql = " WHERE title LIKE '%" . $keyword . "%'";
        }
        $orderby = " ORDER BY ii_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition = array(
            'page' => $page, //int 请求页面 页码
            'limit' => $limit, //int 每页请求个数
            'perpage' => $perpage, //int 每页显示个数
            'wheresql' => $wheresql, //string //条件SQL语句 
            'orderby' => $orderby, //string 排序规则
        );
        $page_result = $this->api('index_index', $page_condition);
        //DEBUG 列表数据返回结构
        /*
          $page_result = array(
          //int 返回结果总数
          'total_rows' => $total_rows,
          //array 一页数据数组
          'page_data' => $page_data
          );
         */
        $page_result = $this->api('index_index', $page_condition);
        if ('json' == $return_type) {
            $return = json_encode($page_result);
        } elseif ('xml' == $return_type) {
            $return = array2xml($page_result);
        }
        //DEBUG 接口响应直接输出返回
        echo $return;
    }
           
}

?>